cc.Class({
    extends: cc.Component,
    
    properties: {        
        gameRoot:{
            default:null,
            type:cc.Node
        },
        
        prepareRoot:{
            default:null,
            type:cc.Node   
        },
        
        _myMJArr:[],
        _chiPaiInfs:[],
        _options:null,
        _chiOptions:null,
        _selectedMJ:null,
        _touchedMJ:null,//滑动那张pai;
        _touched_y:-1,//滑动位移
        _chupaiSprite:[],
        _mjcount:null,
        _gamecount:null,
        _hupaiTips:[],
        _hupaiLists:[],
        _playEfxs:[],
        _opts:[],
        paiNumb:null,
        caiShen:null,
        caiShenTip:null,//
        dizhu:null,//底数
        baiban:29,
        chishuqiang:null,
        // youXianPai:[],
    },
    
    onLoad: function () {
        console.log("-----------------------------onLoad MjGame.js-------");
        if(!cc.vv){
            cc.director.loadScene("loading");
            return;
        }
        this.paiNumb=16;
        this.baiban=29;

        this.addComponent("NoticeTip");
        this.addComponent("GameOver_zw");
        // this.addComponent("DingQue");//定缺换成买/定底。
        this.addComponent("PengGangs");
        this.addComponent("MJRoom");
        this.addComponent("TimePointer");
        this.addComponent("GameResult");
        this.addComponent("Chat");
        this.addComponent("Folds");
        this.addComponent("Brandwall");
        this.addComponent("ReplayCtrl");
        this.addComponent("PopupMgr");
        this.addComponent("HuanSanZhang");
        this.addComponent("ReConnect");
        this.addComponent("Voice");
        this.addComponent("UserInfoShow"); 
        
        this.initView();
        this.initEventHandlers();
        this.addComponent("MaiDi");
        
        this.gameRoot.active = false;
        this.prepareRoot.active = true;
        this.initWanfaLabel();
        this.initCaiShen();
        // this.onGameBeign();   
        cc.vv.audioMgr.playBGM("bgFight.mp3");
        
        this.initGameNetMgrData();
        // if(cc.vv.replayMgr.isReplay()){
        //     this.onGameBeign();
        // }
    },
    
    initView:function(){
        console.log("---------------------initView MjGame-----------------");
        //搜索需要的子节点
        var gameChild = this.node.getChildByName("game");
        
        this.chishuqiang=gameChild.getChildByName("caishuqiang");
        console.log("chishuqiang=" +this.chishuqiang);
        this.chishuqiang.active=false;
        var mjxinxi = gameChild.getChildByName('mjxinxi');
        this._mjcount = mjxinxi.getChildByName('mjcount').getComponent(cc.Label);
        this._mjcount.string = cc.vv.gameNetMgr.numOfMJ + "张";
        this._gamecount = mjxinxi.getChildByName('gamecount').getComponent(cc.Label);
        this._gamecount.string = "" + cc.vv.gameNetMgr.numOfGames + "/" + cc.vv.gameNetMgr.maxNumOfGames + "局";
        
        var myselfChild = gameChild.getChildByName("myself");
        var myholds = myselfChild.getChildByName("holds");
       
        // console.log("myholds.children.length:"+myholds.children.length);
        for(var i = 0; i < myholds.children.length; ++i){
            var sprite = myholds.children[i].getComponent(cc.Sprite);
            var caishen =  myholds.children[i].getChildByName("caishen");
            caishen.active = false;
            this._myMJArr.push(sprite);
            sprite.spriteFrame = null;

            //添加touch事件
            this.setTouchEvent(sprite);
        }
        
        var realwidth = cc.director.getVisibleSize().width;
        myholds.scaleX *= realwidth/1280;
        myholds.scaleY *= realwidth/1280;  
        
        var sides = ["myself","right","up","left"];
        if(cc.vv.setPeople.isERMJ()){//修改成二人
            sides = ["myself","up"];
        }
        cc.log("initView","sides="+sides);
        for(var i = 0; i < sides.length; ++i){
            var side = sides[i];         
            var sideChild = gameChild.getChildByName(side);
            this._hupaiTips.push(sideChild.getChildByName("HuPai"));
            // this._hupaiLists.push(sideChild.getChildByName("hupailist"));
            this._playEfxs.push(sideChild.getChildByName("play_efx").getComponent(cc.Animation));
            this._chupaiSprite.push(sideChild.getChildByName("ChuPai").children[0].getComponent(cc.Sprite));
            
            var opt = sideChild.getChildByName("opt");
            opt.active = false;
            var sprite = opt.getChildByName("pai").getComponent(cc.Sprite);
            var data = {
                node:opt,
                sprite:sprite
            };
            this._opts.push(data);
        }
        
        //本家的选择
        var opts = gameChild.getChildByName("ops");
        var chiOpts=gameChild.getChildByName("chiOps");
        this._options = opts;
        this._chiOptions=chiOpts;
        // chiOpts.active=true;
        this.hideOptions();
        this.hideChupai();
        this.hideChiOptions();        
    },

    initGameNetMgrData:function()
    {
        console.log("initGameNetMgrData of MJGame.js"+cc.vv.gameNetMgr.isGameSync);
        if(cc.vv.gameNetMgr.caishu!=-1)
        {
            this.dizhu.node.active=true;
            this.caiShen.node.active=true;
            this.caiShenTip.node.active=true;
            console.log("onLoad caiShen"+cc.vv.gameNetMgr.caishu);
            this.setSpriteFrameByMJID("M_",this.caiShen,cc.vv.gameNetMgr.caishu);
            this.caiShen.node.active = false;
            this.caiShenTip.spriteFrame=cc.vv.mahjongmgr.getSpriteFrameByMJID("B_",cc.vv.gameNetMgr.caishu);
        }

        if(cc.vv.gameNetMgr.isGameSync)
        {
            console.log("is game_sync");
            cc.vv.gameNetMgr.dispatchEvent('game_sync');
        }

        // console.log("cc.vv.gameNetMgr.gamestate="+cc.vv.gameNetMgr.gamestate);
        if(cc.vv.gameNetMgr.isGameBegin)
        {
            console.log("MjGame send game_begin push");
            cc.vv.gameNetMgr.dispatchEvent('game_begin');
        }

        // console.log("cc.vv.gameNetMgr.gamestate="+cc.vv.gameNetMgr.gamestate);
        if(cc.vv.gameNetMgr.maiOrDingDiArr&&"idle"==cc.vv.gameNetMgr.gamestate)
        {
            var type=cc.vv.gameNetMgr.maiOrDingDiArr[cc.vv.gameNetMgr.seatIndex];
            console.log("initGameNetMgrData of MaiOrDing+"+type);
            // if()
            //只有买底or顶底时，才会发送push.
            if(type==1||type==3)
            {
                cc.vv.gameNetMgr.dispatchEvent('is_mai_or_ding_di',type);
            }

        }
        if(cc.vv.gameNetMgr.isshaizi != null)
        {
            cc.vv.gameNetMgr.dispatchEvent('banker_push',cc.vv.gameNetMgr.isshaizi);
        }
    },

    hideChupai:function(){
        for(var i = 0; i < this._chupaiSprite.length; ++i){
            this._chupaiSprite[i].node.active = false;
        }        
    },
    
    initEventHandlers:function(){
        cc.vv.gameNetMgr.dataEventHandler = this.node;
        
        //初始化事件监听器
        var self = this;
        
        this.node.on('game_holds',function(data){
            console.log("game_holds");
            // self.initMahjongs();
            //   self.checkQueYiMen();
            // self.checkYouXianPai();
        });
        
        //开始游戏后，正常流程。
        this.node.on('game_begin',function(data){
            console.log("game_begin of MJGame");
            self.onGameBeign();
            self.initHoldsYouXian();
            // self.hideAllOptions();
        });
        
        //游戏断线后，重新进行游戏流程。
        this.node.on('game_sync',function(data){
            console.log("game_sync");
            self.onGameBeign();
            self.initHoldsYouXian();
            // self.hideAllOptions();
        });

        this.node.on('refresh_sync_infs',function(data) {
            console.log("refresh_sync_infs");
            console.log(cc.vv.gameNetMgr.gamestate);
            if(cc.vv.gameNetMgr.gamestate='idle') {
                var type=cc.vv.gameNetMgr.maiOrDingDiArr[cc.vv.gameNetMgr.seatIndex];
                console.log("initGameNetMgrData of MaiOrDing+"+type);
                if(type==1||type==2||type==3||type==4) {
                    cc.vv.gameNetMgr.dispatchEvent('initZhuang',cc.vv.gameNetMgr.button);
                }
                //只有买底or顶底时，才会发送push.
                if(type==1||type==3||type==2||type==4)
                {
                    cc.vv.gameNetMgr.dispatchEvent('is_mai_or_ding_di',type);
                }
                return ;
            }
            //下面为playing 状态
            //刷新庄
            cc.vv.gameNetMgr.dispatchEvent('initZhuang',cc.vv.gameNetMgr.button);

            //显示到哪一家摸牌
            if(cc.vv.gameNetMgr.turn!=cc.vv.gameNetMgr.seatIndex) {
                self.initMopai(data.turn,-1);
            }
            for(var i=0;i<cc.vv.gameNetMgr.seats.length;i++) {
                if(i==cc.vv.gameNetMgr.seatIndex){
                    continue;
                }
                if(i==cc.vv.gameNetMgr.turn){
                    continue;
                }
                self.initMopai(i,null);
            }

            //是否刷新自己的牌。
            if(cc.vv.gameNetMgr.turn==cc.vv.gameNetMgr.seatIndex) {
                console.log('refresh myself pai ');
                var seatData = cc.vv.gameNetMgr.seats[cc.vv.gameNetMgr.seatIndex];
                var lackingNum=cc.vv.gameNetMgr.getLen(seatData);
                console.log(seatData.holds);
                if(lackingNum+seatData.holds.length==self.paiNumb+1) {
                    console.log('pai equal 17 initMahjongs');
                    self.initMahjongs();
                }
            }

            //刷新还有几张牌
            self.dizhu.string = cc.vv.gameNetMgr.difen;//底分
            cc.vv.gameNetMgr.dispatchEvent('game_num');//进行到第几局
            cc.vv.gameNetMgr.dispatchEvent('mj_count');//还剩下几张牌。
            self.initWanfaLabel();//玩法。

        });
        
        this.node.on('game_chupai',function(data){
            console.log("game_chupai");
            data = data.detail;
            self.hideChupai();
            // self.checkQueYiMen();
            
            //如果是本家，就是刷新自己的优先牌。
            if(data.turn==cc.vv.gameNetMgr.seatIndex)
            {
                self.checkYouXianPai();
            } else//占亮第17张牌。
            {
                self.initMopai(data.turn,-1);
            }
            //last是上一位出牌的人，不一定是上家。
            for(var i=0;i<cc.vv.gameNetMgr.seats.length;i++) {
                if(i==cc.vv.gameNetMgr.seatIndex){
                    continue;
                }
                if(i==data.turn){
                    continue;
                }
                self.initMopai(i,null);
            }
            // if(data.last != cc.vv.gameNetMgr.seatIndex){
            //     console.log("data.last of initMopai");
            //     self.initMopai(data.last,null);                  
            // }
            // if(!cc.vv.replayMgr.isReplay() && data.turn != cc.vv.gameNetMgr.seatIndex){
            //     console.log("data.turn of initMopai");
            //     self.initMopai(data.turn,-1);
            // }

        });
        
        this.node.on('game_mopai',function(data){
            console.log("game_mopai");
            self.hideChupai();
            self.initMahjongs();
            // data = data.detail;
            // var pai = data.pai;
            // var localIndex = cc.vv.gameNetMgr.getLocalIndex(data.seatIndex);

            // //显示最后一张牌，也就是
            // if(localIndex == 0){
            //     // var index = 13;
            //     console.log("self._myMJArr="+self._myMJArr.length);
            //     var sprite = self._myMJArr[self.paiNumb]; 
            //     self.setSpriteFrameByMJID("M_",sprite,pai,self.paiNumb);
            //     sprite.node.mjId = pai;                
            // }
            // else if(cc.vv.replayMgr.isReplay()){
            //     console.log(" of game_mopai");
            //     self.initMopai(data.seatIndex,pai);
            // }
        });
        
        this.node.on('game_action',function(data){
            console.log("game_action");
            self.showAction(data.detail);
            // self.showAction(data);
        });
        
        this.node.on('hupai',function(data){
            console.log("hupai");
            var data = data.detail;
            console.log("data of hupai of MJGame"+data);
            //如果不是玩家自己，则将玩家的牌都放倒
            var seatIndex = data.seatindex;
            var localIndex = cc.vv.gameNetMgr.getLocalIndex(seatIndex);
            var hupai=data.hupai;
            var isZimo=data.iszimo;
            cc.vv.mahjongmgr.getActionSounds("hu");
            // if()
            if(typeof hupai =="string")
            {
                console.log("hupai is string ");
                hupai=number(hupai);
            }
            // var hupai = self._hupaiTips[localIndex];
            // hupai.active = true;
            
            self.hideAllOptions();
            if(localIndex == 0){
                // self.hideOptions();
                // self.hideChiOptions();

                //显示本家胡牌
                var sprite = self._myMJArr[self.paiNumb];
                sprite.node.mjId = hupai;
                if(hupai!=-1)
                {
                   self.setSpriteFrameByMJID("M_",sprite,hupai);
                   if(sprite.node.getChildByName("caishen").active)
                   {
                       sprite.node.getChildByName("caishen").active = false;
                   }
                }
            }
            var seatData = cc.vv.gameNetMgr.seats[seatIndex];
            
            // if(cc.vv.replayMgr.isReplay() == true && cc.vv.gameNetMgr.conf.type != "xlch"){
            //     var opt = self._opts[localIndex];
            //     opt.node.active = true;
            //     opt.sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID("M_",data.hupai);                
            // }
            
        });
        
        this.node.on('mj_count',function(data){
            self._mjcount.string = cc.vv.gameNetMgr.numOfMJ + "张";
        });
        
        this.node.on('game_num',function(data){
            self._gamecount.string = "" + cc.vv.gameNetMgr.numOfGames + "/" + cc.vv.gameNetMgr.maxNumOfGames + "局";
        });
        
        this.node.on('game_over',function(data){
            console.log("game_over of MJGame"+data.detail);
            // self.gameRoot.active = false;
            // self.prepareRoot.active = true;
            self.initShowOtherHolds(data.detail);

            self.hideOptions();
            self.hideChupai();
            self.hideChiOptions();
        });
        
        
        this.node.on('game_chupai_notify',function(data){
            console.log("game_chupai_notify");
            // self.hideChupai();
            cc.vv.gameNetMgr.dispatchEvent('stop_alarm_push');
            var seatData = data.detail.seatData;
            //如果是自己，则刷新手牌
            if(seatData.seatindex == cc.vv.gameNetMgr.seatIndex){
                // self.initMahjongs();
                var sprite = self._myMJArr[self.paiNumb]; 
                sprite.node.mjId = null;
                sprite.spriteFrame = null;
                sprite.node.active = false;                
            }

            self.initHolds(seatData);

            self.showAllHolds();
            //显示那张牌。
            self.showChupai();
            console.log("data.detail.pai"+data.detail.pai);
            var audioUrl = cc.vv.mahjongmgr.getAudioURLByMJID(data.detail.pai);
            cc.vv.audioMgr.playSFX(audioUrl);
        });
        
        this.node.on('guo_notify',function(data){
            console.log("guo_notify");
            self.hideChupai();
            // self.hideOptions();
            // self.hideChiOptions();
            var seatData = data.detail;
            cc.vv.audioMgr.playSFX("give.mp3");
            // if(cc.vv.gameNetMgr.chu_pai_animation_data){
            //     return;
            // }
            // self.hideAllOptions();

            cc.vv.gameNetMgr.dispatchEvent('stop_alarm_push');
        });
        
        this.node.on('guo_result',function(data){
            // self.hideOptions();
            // self.hideChiOptions();
            self.hideAllOptions();
        });
        
        this.node.on('game_dingque_finish',function(data){
            self.initMahjongs();
        });
        
        this.node.on('peng_notify',function(data){    
            self.hideChupai();
            
            var seatData = data.detail;
            self.initHolds(seatData);
            var localIndex = self.getLocalIndex(seatData.seatindex);
            cc.log("localIndex="+localIndex,"_playEfxs.length="+self._playEfxs.length);
            self._playEfxs[localIndex].play("play_peng");
            // cc.vv.audioMgr.playSFX("nv/peng.mp3");
            cc.vv.mahjongmgr.getActionSounds("peng");
            // self.hideOptions();
            // self.hideChiOptions();

            // cc.vv.gameNetMgr.dispatchEvent('stop_alarm_push');
            self.hideAllOptions();
        });
        
        this.node.on('chi_notify',function(data){    
            self.hideChupai();
            console.log("chi_notify");
            var seatData = data.detail;
            self.initHolds(seatData);
            var localIndex = self.getLocalIndex(seatData.seatindex);
            cc.log("localIndex="+localIndex,"_playEfxs.length="+self._playEfxs.length);

            self._playEfxs[localIndex].play("play_chi");
            // cc.vv.audioMgr.playSFX("nv/chi.mp3");
            cc.vv.mahjongmgr.getActionSounds("chi");

            // self.hideOptions();
            // self.hideChiOptions();
            self.hideAllOptions();
        });

        this.node.on('wait_other',function(data){
            console.log("wait_other");

            // self.hideChupai();
            // self.hideOptions();
            // self.hideChiOptions();
            self.hideAllOptions();
        });
            
        
        this.node.on('gang_notify',function(data){
            console.log("gang_notify");
            self.hideChupai();
            var data = data.detail;
            var seatData = data.seatData;
            var gangtype = data.gangtype;
            self.initHolds(seatData);
            
            var localIndex = self.getLocalIndex(seatData.seatindex);
            cc.log("localIndex="+localIndex,"_playEfxs.length="+self._playEfxs.length);
            self._playEfxs[localIndex].play("play_gang");
            // cc.vv.audioMgr.playSFX("nv/gang.mp3");
            cc.vv.mahjongmgr.getActionSounds("gang");

            // self.hideOptions();
            // self.hideChiOptions();
            self.hideAllOptions();
        });
        
        this.node.on("hangang_notify",function(data){
            console.log("hangang_notify");
            var data = data.detail;
            var localIndex = self.getLocalIndex(data);
            self._playEfxs[localIndex].play("play_gang");
            // cc.vv.audioMgr.playSFX("nv/gang.mp3");
            cc.vv.mahjongmgr.getActionSounds("gang");
            // self.hideOptions();
            // self.hideChiOptions();
            self.hideAllOptions();
        });
        
        this.node.on("game_begin",function(data)
        {
            var data=data.detail;
            console.log("init_caishen:"+data);
            if (self.caiShen==null||self.caiShenTip==null) {
                self.initCaishen();
            }
            
            self.dizhu.node.active=true;
            self.caiShen.node.active=true;
            self.caiShenTip.node.active=true;
            self.setSpriteFrameByMJID("M_",self.caiShen,cc.vv.gameNetMgr.caishu);
            self.caiShen.node.active = false;
            console.log(cc.vv.gameNetMgr.button);
            self.caiShenTip.spriteFrame=cc.vv.mahjongmgr.getSpriteFrameByMJID("B_",cc.vv.gameNetMgr.caishu);
            
        });
        
        this.node.on("youxian_push",function(data)
        {
            // var data=data.detail;
            // console.log("youxian_push");
            // console.log(data);
        });

        this.node.on("is_mai_or_ding_di",function(data)
        {
            console.log("is_mai_or_ding_di of MJGame.js");
            if(data.detail==1||data.detail==3||data.detail==2||data.detail==4)
            {
                self.initMaiOrDingDi();
            }
            cc.find("Canvas/game/caishu/pai").active = false;
            // self.dispatchEvent('is_mai_or_ding_di',data);
        });
        this.node.on("banker_push",function(data)
        {
            console.log("banker_push of MJGame.js");
            // if(data.detail==1||data.detail==3)
            // {
                self.initMaiOrDingDi();
            // }  
            // self.dispatchEvent('is_mai_or_ding_di',data);
        });
        this.node.on("paidun_push",function(data)
        {
            console.log("paidun_push of MJGame.js");
            // if(data.detail==1||data.detail==3)
            // {
                if(!cc.vv.gameNetMgr.ismaidi)
                {
                   self.initMaiOrDingDi(); 
                }
            // }  
            // self.dispatchEvent('is_mai_or_ding_di',data);
        });

        this.node.on("game_over_ready",function(data)
        {
            console.log("game_over_ready of MJGame.js");
            if("playing"==cc.vv.gameNetMgr.gamestate)
            {   
                console.log("game is playing");
                return 
            }
            self.gameRoot.active = false;
            self.prepareRoot.active = true;

        });
    },

    hideAllOptions:function(){
        this.hideOptions();
        this.hideChiOptions();
        cc.vv.gameNetMgr.dispatchEvent('stop_alarm_push');
    },
    
    showChupai:function(){
        var pai = cc.vv.gameNetMgr.chupai; 
        console.log("showChuPai"+pai);
        if( pai >= 0 ){
            var localIndex = this.getLocalIndex(cc.vv.gameNetMgr.turn);
            var sprite = this._chupaiSprite[localIndex];
            sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID("M_",pai);
            sprite.node.active = true;   
        }
    },
    
    addOption:function(btnName,pai){

        for(var i = 0; i < this._options.childrenCount; ++i){
            var child = this._options.children[i]; 
            if(child.name == "op" && child.active == false){
                child.active = true;
                var sprite = child.getChildByName("opTarget").getComponent(cc.Sprite);
                sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID("M_",pai);
                var btn = child.getChildByName(btnName); 
                btn.active = true;
                btn.pai = pai;

                //用于确定选择类型。
                child.btnType=btnName;
                child.opsNum=i;
                return;
            }
        }
    },
    
    addChiOption:function(pai,chipaiArr){
        
        for(var i = 0; i < this._options.childrenCount; ++i){
            var child = this._options.children[i]; 
            if(child.name == "op" && child.active == false){
                child.active = true;
                var sprite = child.getChildByName("opTarget").getComponent(cc.Sprite);
                sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID("M_",pai);
                var btn = child.getChildByName("btnChi"); 
                btn.active = true;
                btn.pai = pai;

                child.btnType="btnChi"
                
                //先清空
                this._chiPaiInfs.length=0;
                
                for(var i=0;i<chipaiArr.length;++i){
                    var data=chipaiArr[i];
                    // console.log("chipaiArr="+data+" chipaiArr.length"+chipaiArr.length);
                    this._chiPaiInfs.push(data);
                }
                return;
            }
        }
        
    },

    addJustChiOption:function(pai,chipaiArr)
    {
        //先清空
        this._chiPaiInfs.length=0;
                
        for(var i=0;i<chipaiArr.length;++i)
        {
            var data=chipaiArr[i];
            // console.log("chipaiArr="+data+" chipaiArr.length"+chipaiArr.length);
            this._chiPaiInfs.push(data);
        }  

             console.log("onOptionClicked is chi");
             //隐藏ops
             if(this._options.active)
             {
                this.hideOptions();
             }
            
            this.hideChiOptions();
            this._chiOptions.active=true;

            var len=this._chiPaiInfs.length/3;
            //不能超过3
            len=len>3?3:len;
            var paiArr=this._chiPaiInfs;
            
             for(var i=0;i<len;i++)
             {
                cc.log("i="+i);
                var j=i+1;
                var name="op_"+j;
                var child=this._chiOptions.getChildByName(name);
                this._chiOptions.getChildByName("New Layout").active=true;
                // cc.log(child.name);
                //显示出来
                child.active=true;
                var pai=child.getChildByName("pai_1").getComponent(cc.Sprite);
                this.setSpriteFrameByMJID("M_",pai,paiArr[i*3+0]); 
                var pai=child.getChildByName("pai_2").getComponent(cc.Sprite);
                this.setSpriteFrameByMJID("M_",pai,paiArr[i*3+1]);
                var pai=child.getChildByName("pai_3").getComponent(cc.Sprite);
                this.setSpriteFrameByMJID("M_",pai,paiArr[i*3+2]);

                child.paiArr=[paiArr[i*3+0],paiArr[i*3+1],paiArr[i*3+2]];
                console.log("chipai Arr="+child.paiArr);
             }

    },
    
    hideOptions:function(data){
        this._options.active = false;
        for(var i = 0; i < this._options.childrenCount; ++i){
            var child = this._options.children[i]; 
            if(child.name == "op"){
                child.active = false;
                child.getChildByName("btnPeng").active = false;
                child.getChildByName("btnGang").active = false;
                child.getChildByName("btnHu").active = false;
                child.getChildByName("btnChi").active=false;
            }
        }
    },
    
    // showOptions:function()
    // {
    //     this._options.active = true;
    //     for(var i = 0; i < this._options.childrenCount; ++i){
    //         var child = this._options.children[i]; 
    //         if(child.name == "op"){
    //             child.active = true;
    //             child.getChildByName("btnPeng").active = true;
    //             child.getChildByName("btnGang").active = true;
    //             child.getChildByName("btnHu").active = true;
    //             child.getChildByName("btnChi").active=true;
    //         }
    //     }
    // },
    
    hideChiOptions:function()
    {
        this._chiOptions.active=false;
        for(var i=0;i<this._chiOptions.childrenCount;i++)
        {
            var child=this._chiOptions.children[i];
            // console.log("child.name"+child.name);
            if(child.name=="btnGuo")//把弃过滤掉。
            {
                continue;
            }
            child.active=false;
        }
    },
    
    showChiOptions:function(){
        console.log("showChiOptions childrenCount"+this._chiOptions.childrenCount);
        this._chiOptions.active=true;
        for(var i=0;i<this._chiOptions.childrenCount;i++)
        {
            var child=this._chiOptions.children[i];
            console.log("child.name"+child.name);
            if(child.name=="btnGuo")//把弃过滤掉。
            {
                continue;
            }
            child.active=true;
        }
    },
    
    showAction:function(data){

        // if(data==null){
        //     return ;
        // }
        this.hideOptions();
        this.hideChiOptions();

        if(data&&(!data.hu)&&(!data.gang)&&(!data.peng)&&(data.chi))
        {
            console.log("just chi option");
            this.addJustChiOption(data.pai, data.chipai);
        }
        else if(data && (data.hu || data.gang || data.peng|| data.chi))
        {          
            //清除吃的选择数组
            this._chiPaiInfs.length=0;
            this._options.active = true;
            //用于记录牌
            // cc.vv.gameNetMgr.paiOfHGPC=data.pai;
            if(data.hu){
                this.addOption("btnHu",data.pai);
            }
            if(data.chi){
                // this.addOption("btnChi",data.chipai);
                this.addChiOption(data.pai, data.chipai);
            }
            if(data.peng){
                this.addOption("btnPeng",data.pai);
            }
            
            if(data.gang){
                for(var i = 0; i < data.gangpai.length;++i){
                    var gp = data.gangpai[i];
                    this.addOption("btnGang",gp);
                }
            }   
        }
    },
    
    initWanfaLabel:function(){
        var wanfa = cc.find("Canvas/dishu/wanfa").getComponent(cc.Label);
        // wanfa.string = cc.vv.gameNetMgr.getWanfa();
        if(cc.vv.gameNetMgr.dishu)
        {
            var str = "";
            if(cc.vv.gameNetMgr.dishu == 1)
            {
                str = "底数2468";
            }
            else if(cc.vv.gameNetMgr.dishu == 2)
            {
                str = "无进阶底";
            }
            else
            {
                console.log("wanfa label erro!");
                return;
            }
            wanfa.string = str;
        }
    },
    
    initCaiShen:function()
    {
        var caiShen= cc.find("Canvas/game/caishu/pai").getComponent(cc.Sprite);
        this.caiShen =caiShen;
        var dizhu=cc.find("Canvas/game/caishu/difen/fen").getComponent(cc.Label);
        this.dizhu=dizhu;
        
        if(caiShen){
            // console.log("initCaiShen ok");
            this.setSpriteFrameByMJID("M_",caiShen,31);
            this.caiShen.node.active=false;
        }else
        {
            console.log("caiShen is null");
        }
        if(dizhu){
            dizhu.string=cc.vv.gameNetMgr.difen;
            this.dizhu.node.active=false;
        }
        
        // console.log("caishen");
        var caiShenTip= cc.find("Canvas/game/caishuqiang/caishu").getComponent(cc.Sprite);
        this.caiShenTip=caiShenTip;
        this.caiShenTip.spriteFrame=cc.vv.mahjongmgr.getSpriteFrameByMJID("B_",31);
        this.caiShenTip.node.active=false;
        
        // this.caiShen.active=false;
    },
    
    initHoldsYouXian:function()
    {
        var str=("cc.vv.gameNetMgr.turn"+cc.vv.gameNetMgr.turn+" localIndex"+cc.vv.gameNetMgr.localIndex);        
        //是否为本家出牌。
        if(cc.vv.gameNetMgr.turn==cc.vv.gameNetMgr.seatIndex)
        {
            str+=" is turn";
            console.log(str);
            this.checkYouXianPai();//用于断线重连时，要检查优先牌。
        }else{
            str+=" is other ";
            console.log(str);
            this.showAllHolds();
        }

    },

    //当出现吃碰杠时，要把对应的手牌隐藏。
    initHolds:function(seatData)
    {
        console.log("initHolds");
        if(seatData.seatindex == cc.vv.gameNetMgr.seatIndex)
        {
            this.initMahjongs();                
        }
        else{
            console.log("initOtherMahjongs of initHolds");
            this.initOtherMahjongs(seatData,"other");
        }
    },

    initHupai:function(localIndex,pai){
        if(cc.vv.gameNetMgr.conf.type == "xlch"){
            var hupailist = this._hupaiLists[localIndex];
            for(var i = 0; i < hupailist.children.length; ++i){
                var hupainode = hupailist.children[i]; 
                if(hupainode.active == false){
                    var pre = cc.vv.mahjongmgr.getFoldPre(localIndex);
                    hupainode.getComponent(cc.Sprite).spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre,pai);
                    hupainode.active = true;
                    break;
                }
            }   
        }
    },
    
    initMaiOrDingDi:function()
    {
        console.log("initMaiOrDingDi");
        //把胡牌显示给隐藏
        // for(var i = 0; i < this._hupaiLists.length; ++i){
        //     for(var j = 0; j < this._hupaiLists[i].childrenCount; ++j){
        //         this._hupaiLists[i].children[j].active = false;
        //     }
        // }

        for(var i = 0; i < cc.vv.gameNetMgr.seats.length; ++i){
            var seatData = cc.vv.gameNetMgr.seats[i];
            var localIndex = cc.vv.gameNetMgr.getLocalIndex(i);        
            var hupai = this._hupaiTips[localIndex];
            //seatData.hued 来确定是否已胡。
            hupai.active = seatData.hued;
            //确定是胡什么。
            if(seatData.hued){
                hupai.getChildByName("sprHu").active = !seatData.iszimo;
                hupai.getChildByName("sprZimo").active = seatData.iszimo;
            }
        }
        
        //隐藏出牌，选择页面，吃选择页面。
        this.hideChupai();
        this.hideOptions();
        this.hideChiOptions();

        this.chishuqiang.active=false;
        var gameChild = this.node.getChildByName("game");

        var seatsName=["myself","right","up","left"];
        if(cc.vv.setPeople.isERMJ()){//修改成二人
            seatsName = ["myself","up"];
        }
        cc.log("initMaiOrDingDi","sides="+seatsName);
        for(var i in seatsName)
        {
            var sideChild = gameChild.getChildByName(seatsName[i]);
            var seat = sideChild.getChildByName("holds");
            seat.active=false;
        }

        //已经开始游戏了，隐藏准备页面，显示麻将页面。
        // console.log("this.gameRoot.active is true");
        this.gameRoot.active = true;
        this.prepareRoot.active = false;


    },

    onGameBeign:function(){
        for(var i = 0; i < cc.vv.gameNetMgr.seats.length; ++i){
            var seatData = cc.vv.gameNetMgr.seats[i];
            var localIndex = cc.vv.gameNetMgr.getLocalIndex(i);        
            var hupai = this._hupaiTips[localIndex];
            //seatData.hued 来确定是否已胡。
            hupai.active = seatData.hued;
            //确定是胡什么。
            if(seatData.hued){
                hupai.getChildByName("sprHu").active = !seatData.iszimo;
                hupai.getChildByName("sprZimo").active = seatData.iszimo;
            }
        }
        
        //隐藏出牌，选择页面，吃选择页面。
        this.hideChupai();
        this.hideOptions();
        this.hideChiOptions();
        var gameChild = this.node.getChildByName("game");
      
        console.log("gamestate="+cc.vv.gameNetMgr.gamestate);
        if(cc.vv.gameNetMgr.gamestate == "" && cc.vv.replayMgr.isReplay() == false){
            console.log("this.prepareRoot.active is true");
            return;
        }

        if(cc.vv.gameNetMgr.gamestate == "idle"){
            console.log("cc.vv.gameNetMgr.gamestate");
            return;
        }
        this.chishuqiang.active=false;

        var seatsName=["myself","right","up","left"];
        if(cc.vv.setPeople.isERMJ()){//修改成二人
            seatsName = ["myself","up"];
        }
        cc.log("onGameBeign","sides="+seatsName);
        var str="";
        for(var i in seatsName)
        {
            str+=" "+seatsName[i];
            var sideChild = gameChild.getChildByName(seatsName[i]);
            var seat = sideChild.getChildByName("holds");
            seat.active=true;
        }
        
        //已经开始游戏了，隐藏准备页面，显示麻将页面。
        console.log(str+" /n"+"this.gameRoot.active is true");
        this.gameRoot.active = true;
        this.prepareRoot.active = false;
        //初始化手牌。
        this.initMahjongs();
        var seats = cc.vv.gameNetMgr.seats;
        for(var i in seats){
            var seatData = seats[i];
            var localIndex = cc.vv.gameNetMgr.getLocalIndex(i);
            if(localIndex != 0){
                if(i == cc.vv.gameNetMgr.turn){
                    console.log(" of cc.vv.gameNetMgr.turn");
                    //会把牌重新设置
                    this.initMopaiAll(i,-1);
                }
                else{
                    console.log(" of cc.vv.gameNetMgr.turn else");
                    this.initMopaiAll(i,null);    
                }
                //隐藏吃、碰，杠
                console.log("initOtherMahjongs of onGameBeign");
                this.initOtherMahjongs(seatData,"other");
            }
        }        

        //如果不是本人先出牌。把第17张牌给隐藏。
        if(cc.vv.gameNetMgr.turn!=cc.vv.gameNetMgr.seatIndex)
        {
            console.log("17pai active is false");
            var sprite = this._myMJArr[this.paiNumb]; 
            sprite.node.mjId = null;
            sprite.spriteFrame = null;
            sprite.node.active = false;
        }        

        // this.showChupai();
        if(cc.vv.gameNetMgr.curaction != null){
            this.showAction(cc.vv.gameNetMgr.curaction);
            cc.vv.gameNetMgr.curaction = null;
        }
        
    },
    
    resetMJSelect:function()
    {
        if(this._selectedMJ)
        {
            this._selectedMJ.y=0;
        }
        this._selectedMJ=null;

        if(this._touchedMJ)
        {
            this._touchedMJ.y=0;
        }
        this._touchedMJ=null;

    },

    onMJClicked:function(event){
        // console.log("onMJClicked event.target"+event.target.mjId);
        if(cc.vv.gameNetMgr.isHuanSanZhang){
            this.node.emit("mj_clicked",event.target);
            return;
        }
        
        //如果不是自己的轮子，则忽略
        if(cc.vv.gameNetMgr.turn != cc.vv.gameNetMgr.seatIndex){
            console.log("not your turn." + cc.vv.gameNetMgr.turn);
            if(this._selectedMJ != null)
            {
                this._selectedMJ.y = 0;
                this._selectedMJ=null;
                return;
            }
            event.target.y = 15;
            this._selectedMJ = event.target;
            return;
        }
        
        for(var i = 0; i < this._myMJArr.length; ++i){
            if(event.target == this._myMJArr[i].node){
                //如果是再次点击，则出牌
                if(event.target == this._selectedMJ){
                    this.shoot(this._selectedMJ.mjId); 
                    this._selectedMJ.y = 0;
                    this._selectedMJ = null;
                    return;
                }
                if(this._selectedMJ != null){
                    this._selectedMJ.y = 0;
                    console.log("this._selectedMJ.y");
                }
                event.target.y = 15;
                console.log("event.target.y="+event.target.y);
                this._selectedMJ = event.target;
                return;
            }
        }
    },

    setTouchEvent:function(sprite)
    {
        var self=this;
        sprite.node.on(cc.Node.EventType.TOUCH_START,
        function(event){
            // console.log("TOUCH_START event.getLocationY="+event.getLocationY()+" mjId="+event.target.mjId);
            if(!self.isTouch(event.target.mjId))
            {
                console.log("this not touch");
                return;
            }

            self._touched_y=-1;

            //点击不同的牌时，把之前的牌设置原位。
            if(self._touchedMJ!=null&&self._touchedMJ!=event.target)
            {
                console.log("TOUCH_START self._touchedMJ.y=0;");
                self._touchedMJ.y=0;
            }
            self._touchedMJ=event.target;
            event.target.start_y=event.getLocationY();
            console.log("TOUCH_START self._touched_y="+self._touched_y);
           
        },this);

        sprite.node.on(cc.Node.EventType.TOUCH_MOVE,
        function(event){
            if(!self.isTouch(event.target.mjId))
            {
                console.log("this not touch");
                return;
            }
            var distance=event.getLocationY()-event.target.start_y;
            self._touched_y=(distance)>15?(distance>150?150:distance):-1;
            // console.log("self._touched_y"+self._touched_y);

        },this);


        var handler=function(event){

            if(!self.isTouch(event.target.mjId))
            {
                console.log("this not touch");
                return;
            }

            if(self._touched_y>15&&(cc.vv.gameNetMgr.turn == cc.vv.gameNetMgr.seatIndex))
            {
                console.log("can chu pai"+event.target.mjId);
                self.shoot(event.target.mjId);
            }
            else
            {
                console.log("can not chu pai"+event.target.mjId);
            }

            console.log("TOUCH_CANCEL event.getLocationY="+event.getLocationY()+" start_y="+event.target.start_y+" mjId="+event.target.mjId);
            var distance=event.getLocationY()-event.target.start_y;
            console.log("distance"+distance+" self._touched_y"+self._touched_y);
            if(distance>15)
            {
                self._touched_y=0;
            }      
            // console.log("self._touched_y"+self._touched_y);
        }

        sprite.node.on(cc.Node.EventType.TOUCH_END,handler,this);

        sprite.node.on(cc.Node.EventType.TOUCH_CANCEL,handler,this);
    },

    isTouch:function(mjId)
    {
        console.log("mjId="+mjId);
        var isCan=true;
        if(cc.vv.gameNetMgr.caishu==mjId)
        {
            console.log("this caishen pai");
            return false;
        }

        var youXianArr=cc.vv.gameNetMgr.youXianPai;
        if(youXianArr&&youXianArr.length>0)
        {
            var str=" youXianPai="+youXianArr;
            //不本人出牌，但可以选择。
            if(cc.vv.gameNetMgr.turn != cc.vv.gameNetMgr.seatIndex)
            {
                return true;
            }
            var idx=cc.vv.gameNetMgr.youXianPai.indexOf(mjId);
            str+=("isTouch of idx="+idx);
            if(-1==idx)
            {
                var mj=""+mjId;
                str+=" mj_string="+mj;
                idx=cc.vv.gameNetMgr.youXianPai.indexOf(mj);
            }
            str+=" idx="+idx;
            // console.log(str);
            return (idx==-1)?false:true;

        }
        return isCan;
    },
    
    //出牌
    shoot:function(mjId){
        if(mjId == null){
            return;
        }
        console.log("send chupai =",mjId);
        cc.vv.net.send('chupai',mjId);

        cc.vv.gameNetMgr.dispatchEvent('stop_alarm_push');

        // this._touchedMJ.y=5;
        this._touched_y=null;
    },
    
    getMJIndex:function(side,index){
        // if(side=="right"){
        //     return 16-index;
        // }
        var self=this;
        if(side == "right" || side == "up"){
            return self.paiNumb - index;
        }
        return index;
    },
    
    //pai为null时，
    initMopai:function(seatIndex,pai){
        console.log("initMopai of MJGame.js");
        var self=this;
        var localIndex = cc.vv.gameNetMgr.getLocalIndex(seatIndex);
        if(cc.vv.setPeople.isERMJ()){
            localIndex=2;
        }
        // cc.log("localIndex="+localIndex);
        var side = cc.vv.mahjongmgr.getSide(localIndex);
        var pre = cc.vv.mahjongmgr.getFoldPre(localIndex);
        // cc.log("side="+side,"pre="+pre);

        var gameChild = this.node.getChildByName("game");
        var sideChild = gameChild.getChildByName(side);
        var holds = sideChild.getChildByName("holds");

        var lastIndex = this.getMJIndex(side,self.paiNumb);
        var nc = holds.children[lastIndex];

        nc.scaleX = 1.0;
        nc.scaleY = 1.0;
                        
        if(pai == null){//隐藏
            nc.active = false;
        }
        else if(pai >= 0){//显示手牌
            nc.active = true;
            if(side == "up"){
                nc.scaleX = 0.1;
                nc.scaleY = 0.1;
                cc.log("initMopai of up pai>0");                    
            }
            var sprite = nc.getComponent(cc.Sprite); 
            sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre,pai);
        }
        else if(pai != null){//显示手牌背面
            nc.active = true;
            if(side == "up"){
                nc.scaleX = 1.0;
                nc.scaleY = 1.0;                    
            }
            var sprite = nc.getComponent(cc.Sprite); 
            sprite.spriteFrame = cc.vv.mahjongmgr.getHoldsEmptySpriteFrame(side);
            // sprite.spriteFrame = cc.vv.mahjongmgr.getEmptySpriteFrame(side);
        }

    },

    initMopaiAll:function(seatIndex,pai){
        console.log("initMopaiArr of MJGame.js");
        var self=this;
        var localIndex = cc.vv.gameNetMgr.getLocalIndex(seatIndex);
        var str="before ="+localIndex;
        //用于二人麻将
        localIndex=cc.vv.setPeople.getReadIdx(localIndex);
        cc.log("str "+"late="+localIndex);

        var side = cc.vv.mahjongmgr.getSide(localIndex);
        var pre = cc.vv.mahjongmgr.getFoldPre(localIndex);
        
        var gameChild = this.node.getChildByName("game");
        var sideChild = gameChild.getChildByName(side);
        var holds = sideChild.getChildByName("holds");

        var lastIndex = this.getMJIndex(side,self.paiNumb);
        var nc = holds.children[lastIndex];

        nc.scaleX = 1.0;
        nc.scaleY = 1.0;
                        
        if(pai == null){
            nc.active = false;
        }
        else if(pai >= 0){
            // nc.active = true;
            // if(side == "up"){
            //     nc.scaleX = 0.73;
            //     nc.scaleY = 0.73;                    
            // }
            // var sprite = nc.getComponent(cc.Sprite); 
            // sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre,pai);
        }
        else if(pai != null){
            nc.active = true;
            if(side == "up"){
                nc.scaleX = 1.0;
                nc.scaleY = 1.0;                    
            }
            var sprite = nc.getComponent(cc.Sprite); 
            sprite.spriteFrame = cc.vv.mahjongmgr.getHoldsEmptySpriteFrame(side);
            // sprite.spriteFrame = cc.vv.mahjongmgr.getEmptySpriteFrame(side);
        }

        var str= "";
        var spriteFrame=cc.vv.mahjongmgr.getHoldsEmptySpriteFrame(side);
        //显示其他16张牌
        for(var i=self.paiNumb-1;i>-1;i--){
            str+=" i="+i;
            var lastIndex = this.getMJIndex(side,i);
            var nc = holds.children[lastIndex];
            nc.scaleX = 1.0;
            nc.scaleY = 1.0;
            nc.active = true;
            var sprite = nc.getComponent(cc.Sprite);
            sprite.spriteFrame = spriteFrame;
        }
        // console.log(str);
    },

    //这个函数有bug ，要排查
    initOtherMahjongs:function(seatData,type){
        var self=this;
        var str=" initOtherMahjongs";
        var localIndex = this.getLocalIndex(seatData.seatindex);
        var str="before ="+localIndex;
        //用于二人麻将
        localIndex=cc.vv.setPeople.getReadIdx(localIndex);
        cc.log("str "+"late="+localIndex);

        if(localIndex == 0){
            console.log("localIndex =0 of initOtherMahjongs");
            return;
        }
        var side = cc.vv.mahjongmgr.getSide(localIndex);
        var game = this.node.getChildByName("game");
        var sideRoot = game.getChildByName(side);
        var sideHolds = sideRoot.getChildByName("holds");

        //取得吃，碰，扛长度。
        var num=cc.vv.gameNetMgr.getLen(seatData,type);
        str+=" num="+num ;       
        for(var i = 0; i < num; ++i){
            var idx = this.getMJIndex(side,i);
            sideHolds.children[idx].active = false;
        }
        // console.log(str);
    },
    
    sortHolds:function(seatData){
        var holds = seatData.holds;
        if(holds)
        {
            //如果手上的牌的数目是2,5,8,11,14，表示最后一张牌是刚摸到的牌
            var mopai = null;
            var l = holds.length 
            if( l == 2 || l == 5 || l == 8 || l == 11 || l == 14||l==17){
                mopai = holds.pop();
            }
            
            // var dingque = seatData.dingque;
            var caishen=cc.vv.gameNetMgr.caishu;
            cc.vv.mahjongmgr.sortMJ(holds,caishen);
            
            //将摸牌添加到最后
            if(mopai != null){
                holds.push(mopai);
            }
            return holds;
            }

            return null;
    },
    
    //把出现的吃/碰/杠的对应的位置的位置隐藏
    initMahjongs:function(){

        var seats = cc.vv.gameNetMgr.seats;
        var seatData = seats[cc.vv.gameNetMgr.seatIndex];       
        var holds = this.sortHolds(seatData);

        if(holds == null){
            console.log("holds is null ");
            return;
        }
        var str=("length"+holds.length+" initMahjongs myself holds="+holds);
        // console.log(str);
        var lackingNum=cc.vv.gameNetMgr.getLen(seatData);
        if(null==lackingNum)
        {
            lackingNum=0;
        }
        str+=" "+("lackingNum="+lackingNum);

        for(var i = 0; i < holds.length; ++i){
            var mjid = holds[i];
            var sprite = this._myMJArr[i + lackingNum];
            if(!sprite){
                return;
            }
            sprite.node.mjId = mjid;
            this.setSpriteFrameByMJID("M_",sprite,mjid);
            //设置财神不能出
            if(cc.vv.gameNetMgr.caishu==mjid)
            {
                str+=" "+("cc.vv.gameNetMgr.caishu="+cc.vv.gameNetMgr.caishu+ " mjid"+mjid);
                sprite.node.getComponent(cc.Button).interactable = false;
                sprite.node.getChildByName("caishen").active = true;//财神显示
            }
            sprite.node.getChildByName("caishen").active = false;
        }
        for(var i = 0; i < lackingNum; ++i){
            var sprite = this._myMJArr[i]; 
            sprite.node.mjId = null;
            sprite.spriteFrame = null;
            sprite.node.active = false;
        } 
        // console.log(str);     
    },

    initShowOtherHolds:function(seats){

        var str="initShowOtherHolds of MJGame.js";
        // console.log(str);
        if(!seats)
        {
            console.log("seat= "+seats);
            return;
        }
        var seatIdx=cc.vv.gameNetMgr.seatIndex;
        cc.log("seatIdx="+seatIdx);
        // str1+=" seatIdx"+seatIdx;

        var sides = ["myself","right","up","left"];
        if(cc.vv.setPeople.isERMJ()){
            sides = ["myself","up"];
        }
        cc.log("initShowOtherHolds "+sides);
        var gameChild = this.node.getChildByName("game");
        
        //从right,up,left
        for(var i=1;i<sides.length;i++)
        {
            var str_i="";
            var sideChild = gameChild.getChildByName(sides[i]);
            var holds = sideChild.getChildByName("holds");//取sprite
            var iconIdx=i;
            if(cc.vv.setPeople.isERMJ()){
                iconIdx=2;
            }
            var pre= cc.vv.mahjongmgr.getFoldPre(iconIdx);//取图片
            var idx=(seatIdx+i)%4;//用于取手牌数据
            if(cc.vv.setPeople.isERMJ()){
                idx=(seatIdx+i)%2;
            }
            var seatData = seats[idx];
            if(!seatData)
            {
                console.log("seatData="+seatData);
                return;
            }

            var lackingNum=cc.vv.gameNetMgr.getLen(seatData,"other");
            str_i+=" Num="+lackingNum;

            var holdsData = this.sortHolds(seatData);
            if(!holdsData)
            {
                console.log(str_i+" holds is null ");
                continue;
            }
            //隐藏吃、碰，杠
            for(var k = 0; k < lackingNum; ++k)
            {
                str_i+=" k="+k;
                var idx = this.getMJIndex(sides[i],k);
                holds.children[idx].active = false;
            }
            str_i+="j<holds.length+lackingNum="+holdsData.length+lackingNum;
            //显示手牌
            for(var j=0;j<holdsData.length;j++)
            {
                str_i+=" h[j]="+holdsData[j];
                var lastIndex = this.getMJIndex(sides[i],j+lackingNum);
                var nc = holds.children[lastIndex];
                nc.scaleX = 1.0;
                nc.scaleY = 1.0;
                if("up"==sides[i]){
                    nc.scaleX = 0.58;
                    nc.scaleY = 0.58;                    
                }
                nc.active = true;
                var sprite = nc.getComponent(cc.Sprite);
                // sprite.spriteFrame = spriteFrame;
                sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre,holdsData[j]);
                
            }
            if((holdsData.length+lackingNum)==this.paiNumb)
            {
                str+="set pai17 active=false"
                var lastIndex = this.getMJIndex(sides[i],this.paiNumb);
                var nc = holds.children[lastIndex];
                nc.active = false;
            }
        }
        // console.log(str);
    },
    
    setSpriteFrameByMJID:function(pre,sprite,mjid){
        sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre,mjid);
        sprite.node.active = true;
    },
    
    //设置为全部手牌可以点击
    showAllHolds:function()
    {
        console.log("showAllHolds");
        //手牌显示
        for(var i = 0; i < this._myMJArr.length; ++i)
        {
            var sprite = this._myMJArr[i]; 
            
            if(sprite.node.mjId != null)
            {
                if(cc.vv.gameNetMgr.caishu==sprite.node.mjId)
                {
                    sprite.node.getComponent(cc.Button).interactable = false;
                    sprite.node.getChildByName("caishen").active = true;
                    continue;
                }
                sprite.node.getChildByName("caishen").active = false;
                sprite.node.getComponent(cc.Button).interactable = true;
            }
        }
    },

    isYouXian:function(mjid,mjArr)
    {
        for(var i=0;i<mjArr.length;i++)
        {
            if(mjid==mjArr[i])
            {
                return true;
            }
        }
        
        return false;
    },
    
    checkYouXianPai:function(data)
    {
        var str=("checkYouXianPai");
        //初始化事件监听器
        var self = this;
        var caishen=cc.vv.gameNetMgr.caishu;
        var seats = cc.vv.gameNetMgr.seats;
        str+=" "+("cc.vv.gameNetMgr.seatIndex="+cc.vv.gameNetMgr.seatIndex);
        var seatData = seats[cc.vv.gameNetMgr.seatIndex];
        var holds=seatData.holds;
        str+" "+("holds"+holds+"youXianPai"+cc.vv.gameNetMgr.youXianPai);

        var isYouXian=false;
        if(cc.vv.gameNetMgr.youXianPai)
        { 
            for(var i=0;i<cc.vv.gameNetMgr.youXianPai.length;i++)
            {
                var idx=holds.indexOf(cc.vv.gameNetMgr.youXianPai[i]);
                // console.log("holds i="+i+" idx="+idx+" pai="+cc.vv.gameNetMgr.youXianPai[i]);
                //如果youXianPai中有财神
                if(caishen==cc.vv.gameNetMgr.youXianPai[i])
                {
                    continue;
                }

                //财神不是风牌时，白板不能优先出。
                var pai=cc.vv.gameNetMgr.youXianPai[i];
                if(29==pai&&caishen<27)
                {
                    cc.vv.gameNetMgr.youXianPai.splice(i,1);
                    str+=("pai="+pai+" caishen="+caishen+" youXianPai"+cc.vv.gameNetMgr.youXianPai);
                    continue;
                }

                if(idx!=-1)
                {
                    isYouXian=true;
                    break;
                }
                str+=" "+("youxianPai reset length=0");
            }

        }
        str+=" "+("isYouXian:"+isYouXian);
        // console.log(str);

        //手牌显示
        for(var i = 0; i < this._myMJArr.length; ++i)
        {
            var sprite = this._myMJArr[i]; 
            
            if(sprite.node.mjId != null)
            {
                // console.log("sprite.node.mjId="+sprite.node.mjId);
                //是否为财神
                if(caishen!=null&&caishen!=-1)
                {
                    if(caishen==sprite.node.mjId)
                    {
                        // console.log("mj is caishen");
                        // console.log("continue");
                        sprite.node.getComponent(cc.Button).interactable = false;
                        sprite.node.getChildByName("caishen").active = true; // 显示财神
                        continue;
                    }

                }
                
                if(isYouXian)
                {
                    if(cc.vv.gameNetMgr.youXianPai)
                    {
                        // console.log("cc.vv.gameNetMgr.youXianPai.length"+cc.vv.gameNetMgr.youXianPai.length);
                        //是否还有优先
                        if(cc.vv.gameNetMgr.youXianPai.length>0)
                        {
                            var mjid=sprite.node.mjId;
                            var paiArr=cc.vv.gameNetMgr.youXianPai;
                            if(self.isYouXian(mjid,paiArr))
                            {
                                sprite.node.getComponent(cc.Button).interactable = true;
                            }
                            else
                            {
                                sprite.node.getComponent(cc.Button).interactable = false;
                            }
                            // console.log("continue");
                            sprite.node.getChildByName("caishen").active = false;
                            continue;
                        }
                    }
                }               
                //youXianPai为空or长度为0时。
                sprite.node.getComponent(cc.Button).interactable = true;
                sprite.node.getChildByName("caishen").active = false;
            }
        }
    
    },
    
    //如果玩家手上还有缺的牌没有打，则只能打缺牌
    checkQueYiMen:function(){
        console.log("checkQueYiMen");
        if(cc.vv.gameNetMgr.conf==null || !cc.vv.gameNetMgr.getSelfData().hued){
            //遍历检查看是否有未打缺的牌 如果有，则需要将不是定缺的牌设置为不可用
            var dingque = 3;
            console.log("checkQueYiMen"+dingque);
            var hasQue = false;
            if(cc.vv.gameNetMgr.seatIndex == cc.vv.gameNetMgr.turn){
                for(var i = 0; i < this._myMJArr.length; ++i){
                    var sprite = this._myMJArr[i];
    //                console.log("sprite.node.mjId:" + sprite.node.mjId);
                    if(sprite.node.mjId != null){
                        var type = cc.vv.mahjongmgr.getMahjongType(sprite.node.mjId);
                        // console.log("sprite.node.isQue"=sprite.node.isQue);
                        if(type == dingque){
                            hasQue = true;
                            break;
                        }
                    }
                }            
            }

    //        console.log("hasQue:" + hasQue);
            for(var i = 0; i < this._myMJArr.length; ++i){
                var sprite = this._myMJArr[i];
                if(sprite.node.mjId != null){
                    var type = cc.vv.mahjongmgr.getMahjongType(sprite.node.mjId);
                    if(hasQue && type != dingque){
                        sprite.node.getComponent(cc.Button).interactable = false;
                    }
                    else{
                        sprite.node.getComponent(cc.Button).interactable = true;
                    }
                }
            }   
        }
        else{
            if(cc.vv.gameNetMgr.seatIndex == cc.vv.gameNetMgr.turn){
                for(var i = 0; i < 14; ++i){
                    var sprite = this._myMJArr[i]; 
                    if(sprite.node.active == true){
                        sprite.node.getComponent(cc.Button).interactable = i == 13;
                    }
                }
            }
            else{
                for(var i = 0; i < 14; ++i){
                    var sprite = this._myMJArr[i]; 
                    if(sprite.node.active == true){
                        sprite.node.getComponent(cc.Button).interactable = true;
                    }
                }
            }
        }
    },
    
    getLocalIndex:function(index){
        cc.log("index="+index,"cc.vv.gameNetMgr.seatIndex"+cc.vv.gameNetMgr.seatIndex);
        var ret = (index - cc.vv.gameNetMgr.seatIndex + 4) % 4;

        if(cc.vv.setPeople.isERMJ()){
            ret = (index - cc.vv.gameNetMgr.seatIndex + 2) % 2;
        }
        cc.log("ret="+ret);
        return ret;
    },
    
    onOptionClicked:function(event){
        console.log(event.target.pai);
        console.log("event.target.name"+event.target.name);
        console.log("onOptionClicked");
        if(event.target.name == "op")
        {
            console.log(event.target.btnType);
            if(event.target.btnType == "btnPeng")
            {
                cc.vv.net.send("peng");
            }
            else if(event.target.btnType == "btnGang")
            {
                cc.vv.net.send("gang",event.target.getChildByName("btnGang").pai);
            }
            else if(event.target.btnType =="btnHu")
            {
                cc.vv.net.send("hu");
            }
            else if(event.target.btnType == "btnChi")
            {
                 console.log("onOptionClicked is chi");
                //隐藏ops
                if(this._options.active)
                {
                    this.hideOptions();
                }
            
                this.hideChiOptions();
                this._chiOptions.active=true;

                var len=this._chiPaiInfs.length/3;
                //不能超过3
                len=len>3?3:len;
                var paiArr=this._chiPaiInfs;
            
                for(var i=0;i<len;i++)
                {
                    // var child=this._chiOptions.children[i];
                    // //显示出来
                    // child.active=true;

                    var j=i+1;
                    var name="op_"+j;
                    var child=this._chiOptions.getChildByName(name);
                    this._chiOptions.getChildByName("New Layout").active=true;
                    
                    //显示出来
                    child.active=true;
                    var pai=child.getChildByName("pai_1").getComponent(cc.Sprite);
                    this.setSpriteFrameByMJID("M_",pai,paiArr[i*3+0]); 
                    var pai=child.getChildByName("pai_2").getComponent(cc.Sprite);
                    this.setSpriteFrameByMJID("M_",pai,paiArr[i*3+1]);
                    var pai=child.getChildByName("pai_3").getComponent(cc.Sprite);
                    this.setSpriteFrameByMJID("M_",pai,paiArr[i*3+2]);

                    child.paiArr=[paiArr[i*3+0],paiArr[i*3+1],paiArr[i*3+2]];
                    console.log("choose chipai Arr="+child.paiArr);
                }
             
                //  for(var i=0;i<this._chiPaiInfs.length;i++)
                //  {
                //     console.log("this._chiPaiInfs"+this._chiPaiInfs[i]); 
                //  }
            }
        }
        else if(event.target.name == "btnGuo")
        {
             console.log("qi pai");
            cc.vv.net.send("guo");
        }
        // if(event.target.name == "btnPeng"){
        //     cc.vv.net.send("peng");
        // }
        // else if(event.target.name == "btnGang"){
        //     cc.vv.net.send("gang",event.target.pai);
        // }
        // else if(event.target.name == "btnHu"){
        //     cc.vv.net.send("hu");
        // }
        // else if(event.target.name == "btnGuo"){
        //     console.log("qi pai");
        //     cc.vv.net.send("guo");
        // }else if(event.target.name == "btnChi"){
        //     console.log("onOptionClicked is chi");
        //     //隐藏ops
        //      if(this._options.active)
        //      {
        //         this.hideOptions();
        //      }
            
        //     this.hideChiOptions();
        //     this._chiOptions.active=true;

        //     var len=this._chiPaiInfs.length/3;
        //     //不能超过3
        //     len=len>3?3:len;
        //     var paiArr=this._chiPaiInfs;
            
        //      for(var i=0;i<len;i++)
        //      {
        //         var child=this._chiOptions.children[i];
        //         //显示出来
        //         child.active=true;
        //         var pai=child.getChildByName("pai_1").getComponent(cc.Sprite);
        //         this.setSpriteFrameByMJID("M_",pai,paiArr[i*3+0]); 
        //         var pai=child.getChildByName("pai_2").getComponent(cc.Sprite);
        //         this.setSpriteFrameByMJID("M_",pai,paiArr[i*3+1]);
        //         var pai=child.getChildByName("pai_3").getComponent(cc.Sprite);
        //         this.setSpriteFrameByMJID("M_",pai,paiArr[i*3+2]);

        //         child.paiArr=[paiArr[i*3+0],paiArr[i*3+1],paiArr[i*3+2]];
        //         console.log("choose chipai Arr="+child.paiArr);
        //      }
             
        //     //  for(var i=0;i<this._chiPaiInfs.length;i++)
        //     //  {
        //     //     console.log("this._chiPaiInfs"+this._chiPaiInfs[i]); 
        //     //  }
        // }
    },
    
    onChiOptionClicked:function(event)
    {
        console.log("onChiOptionClicked paiArr is "+event.target.paiArr);
        cc.vv.net.send("chipai",event.target.paiArr);

        return;
    },
    
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {


        if(this._touchedMJ!=null&&this._touched_y>=0)
        {
            // console.log("this._touched_y="+this._touched_y);            
            this._touchedMJ.y=this._touched_y;
        }
        // if(cc.vv.gameNetMgr.aaSync!=null&&cc.vv.gameNetMgr.aaSync) {
        //     console.log("------------------------------------------------------------------");
        // }
    },
    
    onDestroy:function(){
        console.log("onDestroy");
        cc.vv.audioMgr.playBGM("bgMain.mp3");
        if(cc.vv){
            cc.vv.gameNetMgr.clear();   
        }
    }
});
